using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MPSizectorDotNet
{
	public class MPSizector
	{
		public delegate void MPSizectorConnectionEventCallBackType(ConnectionEventType ConnectionEvent);

		public delegate void MPSizector3DDataCallBackType(byte BufferIndex);

		public delegate void MPSizector2DDataCallBackType(byte BufferIndex, byte PicIndex);

		private const string DLL_NAME = "MPSizectorAPI.dll";

		private const int MP_PRODUCT_NAME_MAX_LEN = 256;

		private const int MP_PRODUCT_SERIAL_NUMBER_MAX_LEN = 256;

		private static float MP_SIZECTOR_DOT_NET_VER = 2.12f;

		public uint SensorWidth = 0u;

		public uint SensorHeight = 0u;

		public uint SensorResolution = 0u;

		public IntPtr m_SizectorInstance = IntPtr.Zero;

		private unsafe MP3DFrameUnmanaged* m_CallBackBuffer3DUnmanaged = (MP3DFrameUnmanaged*)(void*)IntPtr.Zero;

		private unsafe MP2DFrameUnmanaged* m_CallBackBuffer2DUnmanaged = (MP2DFrameUnmanaged*)(void*)IntPtr.Zero;

		private MP3DFrameManaged[] m_CallBackBuffer3DManaged;

		private MP2DFrameManaged[] m_CallBackBuffer2DManaged;

		private MPSizectorConnectionEventCallBackType m_ConnectionEventCallBackDg;

		private MPSizector3DDataCallBackType m_Managed3DCallbackHandlerDg;

		private MPSizector2DDataCallBackType m_Managed2DCallbackHandlerDg;

		private MPSizector3DDataCallBackType m_Managed3DCallbackUserFunDg = null;

		private MPSizector2DDataCallBackType m_Managed2DCallbackUserFunDg = null;

		public bool IsConnected
		{
			get
			{
				return MPSizector.MPSizector_IsConnected(this.m_SizectorInstance);
			}
		}

		public MPDeviceInfo CurrentDeviceInfo
		{
			get
			{
				MPDeviceInfo result = default(MPDeviceInfo);
				MPSizector.MPSizector_GetCurrentDeviceInfo(this.m_SizectorInstance, out result);
				return result;
			}
		}

		public bool AutoReconnect
		{
			set
			{
				MPSizector.MPSizector_SetAutoReconnect(this.m_SizectorInstance, value);
			}
		}

		public bool IsUSB3
		{
			get
			{
				return MPSizector.MPSizector_IsUSB3(this.m_SizectorInstance);
			}
		}

		public bool Callback3DEnable
		{
			set
			{
				if (value)
				{
					MPSizector.MPSizector_Enable3DDataCallBack(this.m_SizectorInstance);
				}
				else
				{
					MPSizector.MPSizector_Enable3DDataCallBack(this.m_SizectorInstance);
				}
			}
		}

		public bool Callback2DEnable
		{
			set
			{
				if (value)
				{
					MPSizector.MPSizector_Enable2DDataCallBack(this.m_SizectorInstance);
				}
				else
				{
					MPSizector.MPSizector_Enable2DDataCallBack(this.m_SizectorInstance);
				}
			}
		}

		public bool HoldState
		{
			get
			{
				bool result = default(bool);
				if (!MPSizector.MPSizector_GetHoldState(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetHoldState(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public WorkingModeType WorkingMode
		{
			get
			{
				WorkingModeType result = default(WorkingModeType);
				if (!MPSizector.MPSizector_GetWorkingMode(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetWorkingMode(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public byte LEDCurrent
		{
			get
			{
				byte result = default(byte);
				if (!MPSizector.MPSizector_GetLEDCurrent(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetLEDCurrent(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public ushort ExposureTime
		{
			get
			{
				ushort result = default(ushort);
				if (!MPSizector.MPSizector_GetExposureTime(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetExposureTime(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public TriggerSourceType TriggerSource
		{
			get
			{
				TriggerSourceType result = default(TriggerSourceType);
				if (!MPSizector.MPSizector_GetTriggerSource(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetTriggerSource(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public byte SpecialFunctionActivated
		{
			get
			{
				byte result = default(byte);
				if (!MPSizector.MPSizector_GetOutputSpecialFunction(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetOutputSpecialFunction(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public byte Output
		{
			get
			{
				byte result = default(byte);
				if (!MPSizector.MPSizector_GetOutput(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetOutput(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public byte Input
		{
			get
			{
				byte result = default(byte);
				if (!MPSizector.MPSizector_GetInput(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
		}

		public bool InitState
		{
			get
			{
				bool result = default(bool);
				if (!MPSizector.MPSizector_GetInitState(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
		}

		public uint InitStateCode
		{
			get
			{
				uint result = default(uint);
				if (!MPSizector.MPSizector_GetInitStateCode(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
		}

		public bool IsBusy
		{
			get
			{
				uint num = default(uint);
				if (!MPSizector.MPSizector_GetDeviceState(this.m_SizectorInstance, out num))
				{
					throw new IOException("Communication Read Failed");
				}
				return num != 0;
			}
		}

		public byte ValidPointThreshold
		{
			get
			{
				byte result = default(byte);
				if (!MPSizector.MPSizector_GetValidPointThreshold(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetValidPointThreshold(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public bool DefaultHoldState
		{
			get
			{
				bool result = default(bool);
				if (!MPSizector.MPSizector_GetDefaultHoldState(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetDefaultHoldState(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public WorkingModeType DefaultWorkingMode
		{
			get
			{
				WorkingModeType result = default(WorkingModeType);
				if (!MPSizector.MPSizector_GetDefaultWorkingMode(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetDefaultWorkingMode(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public byte DefaultLEDCurrent
		{
			get
			{
				byte result = default(byte);
				if (!MPSizector.MPSizector_GetDefaultLEDCurrent(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetDefaultLEDCurrent(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public ushort DefaultExposureTime
		{
			get
			{
				ushort result = default(ushort);
				if (!MPSizector.MPSizector_GetDefaultExposureTime(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetDefaultExposureTime(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public TriggerSourceType DefaultTriggerSource
		{
			get
			{
				TriggerSourceType result = default(TriggerSourceType);
				if (!MPSizector.MPSizector_GetDefaultTriggerSource(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetDefaultTriggerSource(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public byte DefaultSpecialFunctionActivated
		{
			get
			{
				byte result = default(byte);
				if (!MPSizector.MPSizector_GetDefaultOutputSpecialFunction(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetDefaultOutputSpecialFunction(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		public byte DefaultOutput
		{
			get
			{
				byte result = default(byte);
				if (!MPSizector.MPSizector_GetDefaultOutput(this.m_SizectorInstance, out result))
				{
					throw new IOException("Communication Read Failed");
				}
				return result;
			}
			set
			{
				if (MPSizector.MPSizector_SetDefaultOutput(this.m_SizectorInstance, value))
				{
					return;
				}
				throw new IOException("Communication Write Failed");
			}
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern float GetSizectorAPIVersion();

		public static float GetSizectorDotNetVersion()
		{
			return MPSizector.MP_SIZECTOR_DOT_NET_VER;
		}

		private static bool DependencyCheck()
		{
			string text = "";
			bool flag = !File.Exists("OpenTK.dll");
			bool flag2 = !File.Exists("OpenTK.GLControl.dll");
			bool flag3 = !File.Exists("MPSizectorAPI.dll");
			float num = 0f;
			bool flag4 = false;
			text = (flag ? "OpenTK.dll  " : "");
			text += (flag2 ? "OpenTK.GLControl.dll  " : "");
			text += (flag3 ? "MPSizectorAPI.dll  " : "");
			if (flag | flag2 | flag3)
			{
				MessageBox.Show("未找到以下动态链接库文件: \nCannot find following library file: \n\n" + text + "\n\n请将缺少的文件拷贝到执行目录下。\nPlease copy those file to current folder.", "MPSizectorDotNet Dependency Check", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			float sizectorDotNetVersion = MPSizector.GetSizectorDotNetVersion();
			try
			{
				num = MPSizector.GetSizectorAPIVersion();
			}
			catch
			{
				flag4 = true;
			}
			if (flag4)
			{
				MessageBox.Show("MPSizectorAPI.dll版本与MPSizectorDotNet.dll版本不匹配。 \nMPSizectorAPI.dll has a different version with MPSizectorDotNet.dll \n\nMPSizectorAPI.dll : < V2\nMPSizectorDotNet.dll : V" + sizectorDotNetVersion.ToString() + "\n\n请将同一版本的库文件拷贝至执行目录下。\nPlease copy library file of same version to current folder.", "MPSizectorDotNet Dependency Check", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			if (num != sizectorDotNetVersion)
			{
				MessageBox.Show("MPSizectorAPI.dll版本与MPSizectorDotNet.dll版本不匹配。 \nMPSizectorAPI.dll has a different version with MPSizectorDotNet.dll \n\nMPSizectorAPI.dll : V" + num.ToString() + "\nMPSizectorDotNet.dll : V" + sizectorDotNetVersion.ToString() + "\n\n请将同一版本的库文件拷贝至执行目录下。\nPlease copy library file of same version to current folder.", "MPSizectorDotNet Dependency Check", MessageBoxButtons.OK, MessageBoxIcon.Hand);
				return false;
			}
			return true;
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern IntPtr MPSizector_Create();

		public unsafe MPSizector()
		{
			if (MPSizector.DependencyCheck())
			{
				this.m_SizectorInstance = MPSizector.MPSizector_Create();
				return;
			}
			throw new FileNotFoundException();
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern void MPSizector_Delete(IntPtr pIMPSizector);

		~MPSizector()
		{
			MPSizector.MPSizector_Delete(this.m_SizectorInstance);
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_UpdateDeviceList(IntPtr pIMPSizector);

		public bool UpdateDeviceList()
		{
			return MPSizector.MPSizector_UpdateDeviceList(this.m_SizectorInstance);
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern int MPSizector_GetDeviceCount(IntPtr pIMPSizector);

		public int GetDeviceCount()
		{
			return MPSizector.MPSizector_GetDeviceCount(this.m_SizectorInstance);
		}

		[DllImport("MPSizectorAPI.dll", CharSet = CharSet.Unicode)]
		private static extern void MPSizector_GetDeviceInfo(IntPtr pIMPSizector, out MPDeviceInfo pMPDeviceInfo, int Index);

		public MPDeviceInfo GetDeviceInfo(int Index)
		{
			MPDeviceInfo result = default(MPDeviceInfo);
			MPSizector.MPSizector_GetDeviceInfo(this.m_SizectorInstance, out result, Index);
			return result;
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_Open(IntPtr pIMPSizector, MPDeviceInfo DeviceInfo);

		public bool Open(MPDeviceInfo DeviceInfo)
		{
			bool flag = MPSizector.MPSizector_Open(this.m_SizectorInstance, DeviceInfo);
			if (flag)
			{
				this.SensorWidth = this.CurrentDeviceInfo.SensorWidth;
				this.SensorHeight = this.CurrentDeviceInfo.SensorHeight;
			}
			return flag;
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_Reconnect(IntPtr pIMPSizector);

		public bool Reconnect()
		{
			return MPSizector.MPSizector_Reconnect(this.m_SizectorInstance);
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_IsConnected(IntPtr pIMPSizector);

		[DllImport("MPSizectorAPI.dll")]
		private static extern void MPSizector_GetCurrentDeviceInfo(IntPtr pIMPSizector, out MPDeviceInfo pMPDeviceInfo);

		[DllImport("MPSizectorAPI.dll")]
		private static extern void MPSizector_SetAutoReconnect(IntPtr pIMPSizector, bool AutoReconnect);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_IsUSB3(IntPtr pIMPSizector);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetDeviceConnectionChangeCallBack(IntPtr pIMPSizector, MPSizectorConnectionEventCallBackType CallBackFn);

		public bool SetDeviceConnectionChangeCallBack(MPSizectorConnectionEventCallBackType CallBackFn)
		{
			this.m_ConnectionEventCallBackDg = CallBackFn;
			return MPSizector.MPSizector_SetDeviceConnectionChangeCallBack(this.m_SizectorInstance, this.m_ConnectionEventCallBackDg);
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_FireSoftwareTrigger(IntPtr pIMPSizector);

		public bool FireSoftwareTrigger()
		{
			return MPSizector.MPSizector_FireSoftwareTrigger(this.m_SizectorInstance);
		}

		[DllImport("MPSizectorAPI.dll")]
		private unsafe static extern bool MPSizector_Snap3D(IntPtr pIMPSizector, bool SoftwareTrigger, MP3DFrameUnmanaged* Buffer, uint TimeOutMS);

		public unsafe bool Snap3DUnmanaged(bool SoftwareTrigger, MP3DFrameUnmanaged* Buffer, uint TimeOutMS)
		{
			return MPSizector.MPSizector_Snap3D(this.m_SizectorInstance, SoftwareTrigger, Buffer, TimeOutMS);
		}

		public unsafe bool Snap3D(bool SoftwareTrigger, MP3DFrameManaged Buffer, uint TimeOutMS)
		{
			GCHandle gCHandle = GCHandle.Alloc(Buffer.Data, GCHandleType.Pinned);
			MP3DFrameUnmanaged mP3DFrameUnmanaged = default(MP3DFrameUnmanaged);
			mP3DFrameUnmanaged.SensorWidth = 0u;
			mP3DFrameUnmanaged.SensorHeight = 0u;
			mP3DFrameUnmanaged.SensorResolution = 0u;
			mP3DFrameUnmanaged.XMax = 0f;
			mP3DFrameUnmanaged.YMax = 0f;
			mP3DFrameUnmanaged.ZMax = 0f;
			mP3DFrameUnmanaged.Data = (MP3DDataStruct*)(void*)gCHandle.AddrOfPinnedObject();
			GCHandle gCHandle2 = GCHandle.Alloc(mP3DFrameUnmanaged, GCHandleType.Pinned);
			bool result = this.Snap3DUnmanaged(SoftwareTrigger, (MP3DFrameUnmanaged*)(void*)gCHandle2.AddrOfPinnedObject(), TimeOutMS);
			gCHandle2.Free();
			gCHandle.Free();
			return result;
		}

		[DllImport("MPSizectorAPI.dll")]
		private unsafe static extern bool MPSizector_Snap2D(IntPtr pIMPSizector, bool SoftwareTrigger, MP2DFrameUnmanaged* Buffer, uint TimeOutMS);

		public unsafe bool Snap2DUnmanaged(bool SoftwareTrigger, MP2DFrameUnmanaged* Buffer, uint TimeOutMS)
		{
			return MPSizector.MPSizector_Snap2D(this.m_SizectorInstance, SoftwareTrigger, Buffer, TimeOutMS);
		}

		public unsafe bool Snap2D(bool SoftwareTrigger, MP2DFrameManaged Buffer, uint TimeOutMS)
		{
			GCHandle gCHandle = GCHandle.Alloc(Buffer.Data, GCHandleType.Pinned);
			MP2DFrameUnmanaged mP2DFrameUnmanaged = default(MP2DFrameUnmanaged);
			mP2DFrameUnmanaged.SensorWidth = 0u;
			mP2DFrameUnmanaged.SensorHeight = 0u;
			mP2DFrameUnmanaged.SensorResolution = 0u;
			mP2DFrameUnmanaged.XMax = 0f;
			mP2DFrameUnmanaged.YMax = 0f;
			mP2DFrameUnmanaged.ZMax = 0f;
			mP2DFrameUnmanaged.Data = (byte*)(void*)gCHandle.AddrOfPinnedObject();
			GCHandle gCHandle2 = GCHandle.Alloc(mP2DFrameUnmanaged, GCHandleType.Pinned);
			bool result = this.Snap2DUnmanaged(SoftwareTrigger, (MP2DFrameUnmanaged*)(void*)gCHandle2.AddrOfPinnedObject(), TimeOutMS);
			gCHandle2.Free();
			gCHandle.Free();
			return result;
		}

		[DllImport("MPSizectorAPI.dll")]
		private unsafe static extern MP3DFrameUnmanaged* MPSizector_Set3DDataCallBack(IntPtr pIMPSizector, MPSizector3DDataCallBackType CallBackFn, byte BufferSize);

		public unsafe MP3DFrameUnmanaged* Set3DDataCallBackUnmanaged(MPSizector3DDataCallBackType CallBackFn, byte BufferSize)
		{
			return MPSizector.MPSizector_Set3DDataCallBack(this.m_SizectorInstance, CallBackFn, BufferSize);
		}

		public unsafe MP3DFrameManaged[] Set3DDataCallBackManaged(MPSizector3DDataCallBackType CallBackFn, byte BufferSize)
		{
			this.m_Managed3DCallbackHandlerDg = this.Managed3DCallbackHandler;
			this.m_Managed3DCallbackUserFunDg = CallBackFn;
			this.m_CallBackBuffer3DManaged = new MP3DFrameManaged[BufferSize];
			for (int i = 0; i < BufferSize; i++)
			{
				this.m_CallBackBuffer3DManaged[i] = new MP3DFrameManaged(this.SensorWidth, this.SensorHeight);
			}
			this.m_CallBackBuffer3DUnmanaged = this.Set3DDataCallBackUnmanaged(this.m_Managed3DCallbackHandlerDg, BufferSize);
			return this.m_CallBackBuffer3DManaged;
		}

		private unsafe void Managed3DCallbackHandler(byte BufferIndex)
		{
			if (this.m_Managed3DCallbackUserFunDg != null)
			{
				MP3DFrameUnmanaged* callBackBuffer3DUnmanaged = this.m_CallBackBuffer3DUnmanaged;
				MP3DFrameUnmanaged mP3DFrameUnmanaged = callBackBuffer3DUnmanaged[(int)BufferIndex];
				MP3DDataStruct* data = mP3DFrameUnmanaged.Data;
				MP3DDataStruct[] data2 = this.m_CallBackBuffer3DManaged[BufferIndex].Data;
				GCHandle gCHandle = GCHandle.Alloc(data2, GCHandleType.Pinned);
				this.m_CallBackBuffer3DManaged[BufferIndex].SensorWidth = mP3DFrameUnmanaged.SensorWidth;
				this.m_CallBackBuffer3DManaged[BufferIndex].SensorHeight = mP3DFrameUnmanaged.SensorHeight;
				this.m_CallBackBuffer3DManaged[BufferIndex].SensorResolution = mP3DFrameUnmanaged.SensorResolution;
				this.m_CallBackBuffer3DManaged[BufferIndex].XMax = mP3DFrameUnmanaged.XMax;
				this.m_CallBackBuffer3DManaged[BufferIndex].YMax = mP3DFrameUnmanaged.YMax;
				this.m_CallBackBuffer3DManaged[BufferIndex].ZMax = mP3DFrameUnmanaged.ZMax;
				Utils.CopyMemory(gCHandle.AddrOfPinnedObject(), (IntPtr)(void*)data, (uint)(Marshal.SizeOf(typeof(MP3DDataStruct)) * this.SensorWidth * this.SensorHeight));
				gCHandle.Free();
				this.m_Managed3DCallbackUserFunDg(BufferIndex);
			}
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_Enable3DDataCallBack(IntPtr pIMPSizector);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_Disable3DDataCallBack(IntPtr pIMPSizector);

		[DllImport("MPSizectorAPI.dll")]
		private unsafe static extern MP2DFrameUnmanaged* MPSizector_Set2DDataCallBack(IntPtr pIMPSizector, MPSizector2DDataCallBackType CallBackFn, byte BufferSize);

		public unsafe MP2DFrameUnmanaged* Set2DDataCallBackUnmanaged(MPSizector2DDataCallBackType CallBackFn, byte BufferSize)
		{
			return MPSizector.MPSizector_Set2DDataCallBack(this.m_SizectorInstance, CallBackFn, BufferSize);
		}

		public unsafe MP2DFrameManaged[] Set2DDataCallBackManaged(MPSizector2DDataCallBackType CallBackFn, byte BufferSize)
		{
			this.m_Managed2DCallbackHandlerDg = this.Managed2DCallbackHandler;
			this.m_Managed2DCallbackUserFunDg = CallBackFn;
			this.m_CallBackBuffer2DManaged = new MP2DFrameManaged[BufferSize];
			for (int i = 0; i < BufferSize; i++)
			{
				this.m_CallBackBuffer2DManaged[i] = new MP2DFrameManaged(this.SensorWidth, this.SensorHeight);
			}
			this.m_CallBackBuffer2DUnmanaged = this.Set2DDataCallBackUnmanaged(this.m_Managed2DCallbackHandlerDg, BufferSize);
			return this.m_CallBackBuffer2DManaged;
		}

		private unsafe void Managed2DCallbackHandler(byte BufferIndex, byte PicIndex)
		{
			if (this.m_Managed2DCallbackUserFunDg != null)
			{
				MP2DFrameUnmanaged* callBackBuffer2DUnmanaged = this.m_CallBackBuffer2DUnmanaged;
				MP2DFrameUnmanaged mP2DFrameUnmanaged = callBackBuffer2DUnmanaged[(int)BufferIndex];
				byte* data = mP2DFrameUnmanaged.Data;
				byte[] data2 = this.m_CallBackBuffer2DManaged[BufferIndex].Data;
				GCHandle gCHandle = GCHandle.Alloc(data2, GCHandleType.Pinned);
				this.m_CallBackBuffer2DManaged[BufferIndex].SensorWidth = mP2DFrameUnmanaged.SensorWidth;
				this.m_CallBackBuffer2DManaged[BufferIndex].SensorHeight = mP2DFrameUnmanaged.SensorHeight;
				this.m_CallBackBuffer2DManaged[BufferIndex].SensorResolution = mP2DFrameUnmanaged.SensorResolution;
				this.m_CallBackBuffer2DManaged[BufferIndex].XMax = mP2DFrameUnmanaged.XMax;
				this.m_CallBackBuffer2DManaged[BufferIndex].YMax = mP2DFrameUnmanaged.YMax;
				this.m_CallBackBuffer2DManaged[BufferIndex].ZMax = mP2DFrameUnmanaged.ZMax;
				Utils.CopyMemory(gCHandle.AddrOfPinnedObject(), (IntPtr)(void*)data, this.SensorWidth * this.SensorHeight);
				gCHandle.Free();
				this.m_Managed2DCallbackUserFunDg(BufferIndex, PicIndex);
			}
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_Enable2DDataCallBack(IntPtr pIMPSizector);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_Disable2DDataCallBack(IntPtr pIMPSizector);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetHoldState(IntPtr pIMPSizector, out bool HoldState);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetHoldState(IntPtr pIMPSizector, bool HoldState);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetWorkingMode(IntPtr pIMPSizector, out WorkingModeType WorkingMode);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetWorkingMode(IntPtr pIMPSizector, WorkingModeType WorkingMode);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetLEDCurrent(IntPtr pIMPSizector, out byte LEDCurrent);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetLEDCurrent(IntPtr pIMPSizector, byte LEDCurrent);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetExposureTime(IntPtr pIMPSizector, out ushort ExposureTime);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetExposureTime(IntPtr pIMPSizector, ushort ExposureTime);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetTriggerSource(IntPtr pIMPSizector, out TriggerSourceType TriggerSource);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetTriggerSource(IntPtr pIMPSizector, TriggerSourceType TriggerSource);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetOutputSpecialFunction(IntPtr pIMPSizector, out byte SpecialFunctionActivated);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetOutputSpecialFunction(IntPtr pIMPSizector, byte SpecialFunctionActivated);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetOutput(IntPtr pIMPSizector, out byte State);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetOutput(IntPtr pIMPSizector, byte State);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetInput(IntPtr pIMPSizector, out byte State);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SoftwareReset(IntPtr pIMPSizector);

		public bool SoftwareReset()
		{
			return MPSizector.MPSizector_SoftwareReset(this.m_SizectorInstance);
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetInitState(IntPtr pIMPSizector, out bool IsInitDone);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetInitStateCode(IntPtr pIMPSizector, out uint StateCode);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetDeviceState(IntPtr pIMPSizector, out uint State);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetValidPointThreshold(IntPtr pIMPSizector, out byte ValidPointThreshold);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetValidPointThreshold(IntPtr pIMPSizector, byte ValidPointThreshold);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_LoadDefaultSettings(IntPtr pIMPSizector);

		public bool LoadDefaultSettings()
		{
			return MPSizector.MPSizector_LoadDefaultSettings(this.m_SizectorInstance);
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SaveDefaultSettings(IntPtr pIMPSizector);

		public bool SaveDefaultSettings()
		{
			return MPSizector.MPSizector_SaveDefaultSettings(this.m_SizectorInstance);
		}

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetDefaultHoldState(IntPtr pIMPSizector, out bool HoldState);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetDefaultHoldState(IntPtr pIMPSizector, bool HoldState);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetDefaultWorkingMode(IntPtr pIMPSizector, out WorkingModeType WorkingMode);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetDefaultWorkingMode(IntPtr pIMPSizector, WorkingModeType WorkingMode);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetDefaultLEDCurrent(IntPtr pIMPSizector, out byte LEDCurrent);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetDefaultLEDCurrent(IntPtr pIMPSizector, byte LEDCurrent);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetDefaultExposureTime(IntPtr pIMPSizector, out ushort ExposureTime);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetDefaultExposureTime(IntPtr pIMPSizector, ushort ExposureTime);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetDefaultTriggerSource(IntPtr pIMPSizector, out TriggerSourceType TriggerSource);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetDefaultTriggerSource(IntPtr pIMPSizector, TriggerSourceType TriggerSource);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetDefaultOutputSpecialFunction(IntPtr pIMPSizector, out byte SpecialFunctionActivated);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetDefaultOutputSpecialFunction(IntPtr pIMPSizector, byte SpecialFunctionActivated);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_GetDefaultOutput(IntPtr pIMPSizector, out byte State);

		[DllImport("MPSizectorAPI.dll")]
		private static extern bool MPSizector_SetDefaultOutput(IntPtr pIMPSizector, byte State);
	}
}
